/*
    This file is part of Lua Invoke.
    Copyright 2005-2007 Rafael de Oliveira Jannone <rafael@jannone.org>

    Lua Invoke is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* type representation */

#include "type.h"

/* simple parser for function signatures, returns a pointer to the next token */
const char* 
type_read(type_t* t, const char* from)
{
	/* FIXME: maybe remove those else's and memset the whole thing anyway? */
	while (*from == ',' || *from == ' ')
		++from;
	if (!*from)
		return from;
	if (*from == 'r') {
		t->ref = true;
		++from;
	} else {
		t->ref = false;
	}
	if (*from == 'a') {
		t->array = true;
		++from;
	} else {
		t->array = false;
	}
	if (*from == 'u') {
		t->unsign = true;
		++from;
	} else {
		t->unsign = false;
	}
	if (*from) {
		t->type = *from++;
	} else {
		t->type = '\0';
	}
	return from;
}

int
type_sizeof(type_t* t)
{
	int sz = 0;
	switch (t->type) {
		case '!':
			sz = sizeof(void*);
			break;
			
		case 'c':
			sz = sizeof(char*);
			break;

		case 'z':
			sz = sizeof(short);
			break;

		case 'i':
			sz = sizeof(int);
			break;
			
		case 'l':
			sz = sizeof(long);
			break;

		case 'L':
			sz = sizeof(long long);
			break;
		
		case 'f':
			sz = sizeof(float);
			break;
		
		case 's':
			sz = sizeof(const char *);
			break;
		
		case 'd':
			sz = sizeof(double);
			break;
			
		case 'P':
		case 'p':
			sz = sizeof(long*);
			break;
	}
	return sz;
}
